﻿<phone:PhoneApplicationPage x:Name="AppPage"
                            x:Class="Strava1.MainPage"
                            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                            xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
                            xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
                            xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
                            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                            xmlns:tk="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
                            xmlns:map="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
                            xmlns:mapCore="clr-namespace:Microsoft.Phone.Controls.Maps.Core;assembly=Microsoft.Phone.Controls.Maps"
                            xmlns:local="clr-namespace:Strava1"
                            mc:Ignorable="d"
                            d:DesignWidth="480"
                            d:DesignHeight="800"
                            d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
                            FontFamily="{StaticResource PhoneFontFamilyNormal}"
                            FontSize="{StaticResource PhoneFontSizeNormal}"
                            Foreground="{StaticResource PhoneForegroundBrush}"
                            SupportedOrientations="Portrait"
                            Orientation="Portrait"
                            shell:SystemTray.IsVisible="True">
    <shell:SystemTray.ProgressIndicator>
        <shell:ProgressIndicator IsIndeterminate="{Binding Busy}"
                                 IsVisible="{Binding Busy}"
                                 Text="{Binding BusyMessage}" />
    </shell:SystemTray.ProgressIndicator>

    <Grid x:Name="LayoutRoot"
          Background="Transparent">

        <controls:Pivot Title="STRAVA VIEWER"
                        SelectionChanged="Pivot_SelectionChanged">
            <controls:Pivot.Background>
                <ImageBrush ImageSource="SplashScreenImage2.png"
                            Opacity="0.75" />
            </controls:Pivot.Background>

            <!--Panorama item one-->
            <!--<controls:PivotItem Header="Dashboard">
                <StackPanel>
                    <TextBlock Text=""
                </StackPanel>
            </controls:PivotItem>-->
            <controls:PivotItem Header="rides">
                <!--Double line list with text wrapping-->
                <Grid>
                    <ListBox x:Name="ridesLB"
                             DataContext="{Binding RidesCVS}"
                             ItemsSource="{Binding}"
                             tk:TiltEffect.IsTiltEnabled="True">
                        <tk:ContextMenuService.ContextMenu>
                            <tk:ContextMenu>
                                <tk:MenuItem Header="date"
                                             Click="SetRideOrder_Click" />
                                <tk:MenuItem Header="distance"
                                             Click="SetRideOrder_Click" />
                                <tk:MenuItem Header="elevation"
                                             Click="SetRideOrder_Click" />
                                <tk:MenuItem Header="average speed"
                                             Click="SetRideOrder_Click" />
                                <tk:MenuItem Header="name"
                                             Click="SetRideOrder_Click" />
                            </tk:ContextMenu>
                        </tk:ContextMenuService.ContextMenu>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="12,0,0,10">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Name}"
                                               TextWrapping="Wrap"
                                               Grid.ColumnSpan="2"
                                               FontSize="{StaticResource PhoneFontSizeMedium}" />

                                    <StackPanel Orientation="Horizontal"
                                                Grid.Row="1"
                                                Grid.ColumnSpan="2">
                                        <TextBlock Text="overall: "
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding OverallFirstPositionCount, StringFormat='{}{0}, '}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding OverallSecondPositionCount, StringFormat='{}{0}, '}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding OverallThirdPositionCount, StringFormat='{}{0}, '}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding OverallOtherPositionCount}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                    </StackPanel>

                                    <StackPanel Orientation="Horizontal"
                                                Grid.Row="2"
                                                Grid.ColumnSpan="2">
                                        <TextBlock Text="my results: "
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding MyFirstPositionCount, StringFormat='{}{0}, '}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding MySecondPositionCount, StringFormat='{}{0}, '}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding MyThirdPositionCount, StringFormat='{}{0}, '}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <TextBlock Text="{Binding MyOtherPositionCount}"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                    </StackPanel>

                                    <TextBlock Text="{Binding Detail.StartDate, StringFormat='{}{0:D}'}"
                                               Grid.Row="3"
                                               Foreground="{StaticResource PhoneSubtleBrush}" />
                                    <TextBlock Text="{Binding Detail.Distance, Converter={StaticResource MeterToKMConverter}, StringFormat='{}{0:n1}km'}"
                                               Grid.Row="3"
                                               Grid.Column="1"
                                               Foreground="{StaticResource PhoneSubtleBrush}" />
                                    <Grid Grid.Row="4"
                                          Grid.ColumnSpan="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>

                                        <TextBlock Text="{Binding Detail.AverageSpeed, Converter={StaticResource MPSToMPHConverter}, StringFormat='{}{0:n1}mph'}"
                                                   Foreground="{StaticResource PhoneSubtleBrush}" />
                                        <TextBlock Text="{Binding Detail.ElevationGain, Converter={StaticResource MeterToFeetConverter}, StringFormat='{}{0:n0}ft'}"
                                                   Grid.Column="1"
                                                   HorizontalAlignment="Center"
                                                   Foreground="{StaticResource PhoneSubtleBrush}" />
                                        <TextBlock Text="{Binding Detail.Bike.Name}"
                                                   Grid.Column="2"
                                                   HorizontalAlignment="Right"
                                                   Foreground="{StaticResource PhoneSubtleBrush}" />
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <Button Visibility="Collapsed"
                            Content="{Binding Rides.Count}"
                            VerticalAlignment="Bottom"
                            Click="Button_Click" />
                </Grid>
            </controls:PivotItem>

            <!--Panorama item two-->
            <!--Use 'Orientation="Horizontal"' to enable a panel that lays out horizontally-->
            <controls:PivotItem Header="segments">
                <!--Double line list with image placeholder and text wrapping-->
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal"
                                Margin="12,0,0,5">
                        <TextBlock Text="{Binding SegFirstCount, StringFormat='{}{0} x 1st,    '}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                        <TextBlock Text="{Binding SegSecondCount, StringFormat='{}{0} x 2nd,    '}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                        <TextBlock Text="{Binding SegThirdCount, StringFormat='{}{0} x 3rd,    '}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                        <TextBlock Text="{Binding SegOtherCount, StringFormat='{}{0} x other'}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                    </StackPanel>
                    <ListBox tk:TiltEffect.IsTiltEnabled="True"
                             Grid.Row="1"
                             DataContext="{Binding SegmentsCVS}"
                             ItemsSource="{Binding}"
                             SelectionChanged="Segments_SelectionChanged">
                        <tk:ContextMenuService.ContextMenu>
                            <tk:ContextMenu>
                                <tk:MenuItem Header="placing"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="relative power"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="VAM"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="climb category"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="distance"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="gradient"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="time behind"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                                <tk:MenuItem Header="name"
                                             FontSize="{StaticResource PhoneFontSizeMedium}"
                                             Click="SetSegmentOrder_Click" />
                            </tk:ContextMenu>
                        </tk:ContextMenuService.ContextMenu>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="0,0,0,10">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal"
                                                    Width="100">
                                            <TextBlock Text="{Binding TopPlacing, StringFormat='{}{0}/', Converter={StaticResource CountMaxConverter}}"
                                                       Margin="12,-6,0,0"
                                                       FontSize="{StaticResource PhoneFontSizeLarge}" />
                                            <TextBlock Text="{Binding PlacingCount, Converter={StaticResource CountMaxConverter}}"
                                                       Margin="0,-6,12,0"
                                                       FontSize="{StaticResource PhoneFontSizeLarge}" />
                                        </StackPanel>
                                        <TextBlock Text="{Binding TimeDifferenceToBest, Converter={StaticResource TimeBehindLeaderConverter}}"
                                                   Margin="12,-6,0,0"
                                                   Foreground="{StaticResource PhoneSubtleBrush}"/>
                                        <TextBlock Text="{Binding MyFastestEffort.Ride.Detail.Bike.Name}"
                                                   Margin="12,-6,0,0"
                                                   Foreground="{StaticResource PhoneSubtleBrush}" />
                                    </StackPanel> 
                                    
                                    <StackPanel Grid.Column="1">
                                        <TextBlock Text="{Binding Name}"
                                                   TextWrapping="Wrap"
                                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition />
                                                <ColumnDefinition />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>

                                            <TextBlock Text="{Binding Distance, Converter={StaticResource MeterToKMConverter}, StringFormat='{}{0:n2}km'}"
                                                       Foreground="{StaticResource PhoneSubtleBrush}" />
                                            <TextBlock Text="{Binding ElevationDifference, Converter={StaticResource MeterToFeetConverter}, StringFormat='{}{0:n0}ft'}"
                                                       Grid.Column="1"
                                                       HorizontalAlignment="Center"
                                                       Foreground="{StaticResource PhoneSubtleBrush}" />
                                            <TextBlock Text="{Binding AverageGrade, StringFormat='{}{0:n1}%'}"
                                                       Grid.Column="2"
                                                       HorizontalAlignment="Right"
                                                       Foreground="{StaticResource PhoneSubtleBrush}" />
                                        </Grid>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>

                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding MyVAM, StringFormat='VAM: {0:n0}'}"
                                                           Foreground="{StaticResource PhoneSubtleBrush}" />
                                                <TextBlock Text="{Binding KOMVAM, StringFormat='/{0:n0}'}"
                                                           Foreground="{StaticResource PhoneSubtleBrush}" />
                                            </StackPanel>

                                            <StackPanel Orientation="Horizontal"
                                                        Grid.Column="1"
                                                        HorizontalAlignment="Right">
                                                <TextBlock Text="{Binding MyRelativePower, StringFormat='RP: {0:n0}'}"
                                                           Foreground="{StaticResource PhoneSubtleBrush}" />
                                                <TextBlock Text="{Binding KOMRelativePower, StringFormat='/{0:n0}'}"
                                                           Foreground="{StaticResource PhoneSubtleBrush}" />
                                            </StackPanel>
                                        </Grid>
                                    </StackPanel>
                                    <TextBlock Text="{Binding ClimbCategory, Converter={StaticResource JustIntConverter}}"
                                               Width="40"
                                               TextAlignment="Right"
                                               Grid.Column="2"
                                               FontSize="{StaticResource PhoneFontSizeLarge}" />
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </controls:PivotItem>
            <controls:PivotItem Header="alerts">
                <ListBox x:Name="alertsLB"
                         tk:TiltEffect.IsTiltEnabled="True"
                         DataContext="{Binding AlertsCVS}"
                         ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="12,0,0,10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding Message}"
                                           TextWrapping="Wrap"
                                           FontSize="{StaticResource PhoneFontSizeMedium}" />
                                <TextBlock Text="{Binding FoundDateTime, StringFormat='{}{0:D}'}"
                                           Grid.Row="1"
                                           Foreground="{StaticResource PhoneSubtleBrush}" />
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </controls:PivotItem>
            <controls:PivotItem Header="explorer"
                                Loaded="Explorer_Loaded">
                <map:Map Grid.Row="5"
                         Margin="0,10"
                         x:Name="SegmentExplorerMap"
                         Grid.ColumnSpan="3"
                         CredentialsProvider="AoOAVXaPhN9YePJC_ptD_S4M42CEVLKGJ4lxiIksAOBaPdJtE6havx99boLZn9G8"
                         CopyrightVisibility="Collapsed"
                         LogoVisibility="Collapsed">
                    <map:Map.Mode>
                        <mapCore:MercatorMode />
                    </map:Map.Mode>
                    <map:Map.Children>
                        <map:MapTileLayer>
                            <map:MapTileLayer.TileSources>
                                <local:OpenCycleMap />
                            </map:MapTileLayer.TileSources>
                        </map:MapTileLayer>
                        <map:MapItemsControl ItemsSource="{Binding ExplorerSegments}">
                            <map:MapItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <map:MapPolyline Locations="{Binding Locations}"
                                                     Stroke="Green"
                                                     StrokeThickness="4"/>
                                </DataTemplate>
                            </map:MapItemsControl.ItemTemplate>
                        </map:MapItemsControl>
                    </map:Map.Children>

                </map:Map>
            </controls:PivotItem>
            <!--<controls:PivotItem Header="All Segments">
            </controls:PivotItem>-->
        </controls:Pivot>
    </Grid>
    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True"
                              IsMenuEnabled="True"
                              Mode="Default"
                              Opacity="0.75">
            <shell:ApplicationBarIconButton IconUri="Images/appbar.addpicture.rest.png"
                                            Text="new courses"
                                            Click="NewCourses_Click" />
            <shell:ApplicationBarIconButton IconUri="Images/appbar.food.pizza.rest.png"
                                            Text="top 3 segment placings"
                                            Click="TopSegmentPlacings_Click" />
            <shell:ApplicationBarIconButton IconUri="Images/appbar.animal.sheep.rest.png"
                                            Text="new segments"
                                            Click="NewSegments_Click" />
            <shell:ApplicationBar.MenuItems>
                <!--<shell:ApplicationBarMenuItem Text="Refresh"
                                              Click="Refresh_Click" />
                <shell:ApplicationBarMenuItem Text="check for new courses"
                                              Click="NewCourses_Click" />
                <shell:ApplicationBarMenuItem Text="check segment placings"
                                              Click="SegmentPlacings_Click" />
                <shell:ApplicationBarMenuItem Text="check for new segments"
                                              Click="NewSegments_Click" />-->
                <shell:ApplicationBarMenuItem Text="check all segment placings"
                                              Click="SegmentPlacings_Click" />
                <shell:ApplicationBarMenuItem Text="Settings"
                                              Click="Settings_Click" />
                <shell:ApplicationBarMenuItem Text="Clear alerts"
                                              Click="ClearAlerts_Click" />
                <shell:ApplicationBarMenuItem Text="Clear full cache"
                                              Click="ClearCache_Click" />
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>